package com.bona.dubbo.system.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.bona.dubbo.system.IGenericDubboSV;
import com.bona.service.common.GenericService;
import com.bona.util.common.StringUtil;
import com.bona.util.db.SqlUtil;

/**
 * 共用jdbc操作方法
 * @author tbzeng
 *
 */
public class GenericDubboSVImpl implements IGenericDubboSV {

	@Autowired
	private GenericService genericService;
	
	@Override
	public Map<String, Object> queryForMap(String sql) {
		return genericService.queryForMap(sql);
	}
	
	@Override
	public Map<String, Object> queryForMap(String sql, Object[] args) {
		return genericService.queryForMap(sql, args);
	}
	
	@Override
	public int queryForInt(String sql) {
		return genericService.queryForInt(sql);
	}
	
	@Override
	public int queryForInt(String sql, Object[] args) {
		return genericService.queryForInt(sql, args);
	}
	
	@Override
	public <T> T queryForObject(String sql, Class<T> requiredType) {
		return genericService.queryForObject(sql, requiredType);
	}
	
	@Override
	public <T> T queryForObject(String sql, Object[] args, Class<T> requiredType) {
		return genericService.queryForObject(sql, args, requiredType);
	}
	
	@Override
	public List<Map<String, Object>> queryForList(String sql) {
		return genericService.queryForList(sql);
	}
	
	@Override
	public <T> List<T> queryForList(String sql, Class<T> elementType) {
		return genericService.queryForList(sql, elementType);
	}
	
	@Override
	public <T> List<T> queryForList(String sql, Object[] args, Class<T> elementType) {
		return genericService.queryForList(sql, args, elementType);
	}
	
	@Override
	public List<Map<String, Object>> queryForList(String sql, Object[] args) {
		return genericService.queryForList(sql, args);
	}
	
	@Override
	public int update(String sql) {
		return genericService.update(sql);
	}
	
	@Override
	public int update(String sql, Object[] args) {
		return genericService.update(sql, args);
	}
	 
	@Override
	public int[] batchUpdate(String[] sql) {
		return genericService.batchUpdate(sql);
	}
	
	@Override
	public int[] batchUpdate(String sql, List<Object[]> batchArgs) {
		return genericService.batchUpdate(sql, batchArgs);
	}
	
	private boolean checkSqlDelete(String sql) {
		if (StringUtil.isEmpty(sql)) return true;
		sql = sql.trim().toLowerCase();
		return sql.startsWith("delete");
	}
	
	@Override
	public Integer queryBySqlCount(String sql) {
		if (checkSqlDelete(sql)) return 0;
		return genericService.queryForInt(SqlUtil.getCountSql(sql));
	}
	
	@Override
	public Integer queryBySqlCount(String sql, Object[] args) {
		if (checkSqlDelete(sql)) return 0;
		return genericService.queryForInt(SqlUtil.getCountSql(sql), args);
	}
	
	@Override
	public List<Map<String, Object>> queryBySqlPage(String sql, int pageNo, int rows) {
		if (checkSqlDelete(sql)) return null;
		sql = SqlUtil.getOraclePageSql(sql, pageNo, rows);
		return genericService.queryForList(sql);
	}
	
	@Override
	public List<Map<String, Object>> queryBySqlPage(String sql, int pageNo, int rows, Object[] args) {
		if (checkSqlDelete(sql)) return null;
		sql = SqlUtil.getOraclePageSql(sql, pageNo, rows);
		return genericService.queryForList(sql, args);
	}
}
